package com.dzl.goods;

import com.alibaba.fastjson.JSONObject;
import com.dzl.asynthread.SyncInitDataServiceImpl;
import com.dzl.base.controller.BaseController;
import com.dzl.constant.CommonConstants;
import com.dzl.goods.pojo.WelfareGoodsClassify;
import com.dzl.goods.pojo.query.WelfareGoodsClassifyQuery;
import com.dzl.goods.pojo.vo.WelfareGoodsClassifyVo;
import com.dzl.goods.service.WelfareGoodsClassifyService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 商品分类表(WelfareGoodsClassify)表控制层
 * @author caobucao
 * @date 2021-06-01 11:33:09
 */
@Api(value = "平台端-商品分类管理", tags = "平台端-商品分类管理")
@RestController
@RequestMapping("/welfareGoodsClassify")
public class WelfareGoodsClassifyController extends BaseController {

    @Autowired
    private WelfareGoodsClassifyService service;
    @Autowired
    private SyncInitDataServiceImpl syncInitDataServiceImpl;

    /**
     * 平台端获取商品分类树-级联下拉选专用
     * @date 2021/6/1 11:53
     * @author caobucao
     * @param
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.goods.pojo.vo.WelfareGoodsClassifyVo>>
     */
    @ApiOperation(value = "平台端-获取商品分类树-级联下拉选专用", notes = "平台端-获取商品分类树-级联下拉选专用")
    @GetMapping("getGoodsClassifiesForSelect")
    public JsonResult<List<Map<String, Object>>> getGoodsClassifiesForSelect() {
        List<Map<String, Object>> list = service.getGoodsClassifiesForSelect(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID);
        return JsonResult.success(list);
    }

    /**
     * 平台端获取商品分类树
     * @date 2021/6/1 11:53
     * @author caobucao
     * @param
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.goods.pojo.vo.WelfareGoodsClassifyVo>>
     */
    @ApiOperation(value = "平台端-获取商品分类树", notes = "平台端-获取商品分类树")
    @GetMapping("getGoodsClassifies")
    public JsonResult<List<WelfareGoodsClassifyVo>> getGoodsClassifies() {
        List<WelfareGoodsClassifyVo> list = service.getGoodsClassify(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID);
        return JsonResult.success(list);
    }

    /**
     * 平台端获取右侧分页列表
     * @date 2021/6/1 14:05
     * @author caobucao
     * @param query  查询条件
     * @return com.dzl.util.PageUtils<com.dzl.goods.pojo.vo.WelfareGoodsClassifyVo>
     */
    @ApiOperation(value = "平台端-获取商品分类树", notes = "平台端-获取商品分类树")
    @GetMapping("getGoodsClassifyPage")
    public PageUtils<WelfareGoodsClassifyVo> getGoodsClassifyPage(WelfareGoodsClassifyQuery query) {
        query.setEnterpriseId(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID);
        List<WelfareGoodsClassifyVo> list = service.getGoodsClassifyPage(query);
        PageInfo<WelfareGoodsClassifyVo> pageInfo = new PageInfo<>(list);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 获取指定等级的所有节点
     * @date 2021/6/2 10:57
     * @author caobucao
     * @param level 指定等级
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.goods.pojo.WelfareGoodsClassify>>
     */
    @ApiOperation(value = "平台端-获取指定等级的所有节点", notes = "平台端-获取指定等级的所有节点")
    @GetMapping("getClassifyListByLevel")
    public JsonResult<List<WelfareGoodsClassify>> getClassifyListByLevel(Integer level) {
        WelfareGoodsClassify welfareGoodsClassify = new WelfareGoodsClassify();
        welfareGoodsClassify.setLevel(level);
        welfareGoodsClassify.setStatus(CommonConstants.YES);
        welfareGoodsClassify.setEnterpriseId(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID);
        List<WelfareGoodsClassify> list = service.select(welfareGoodsClassify);
        return JsonResult.success(list);
    }

    /**
     * 获取当前节点的父级节点
     * @date 2021/6/2 14:25
     * @author caobucao
     * @param id 当前节点id
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.goods.pojo.WelfareGoodsClassify>>
     */
    @ApiOperation(value = "平台端-获取当前节点的父级节点", notes = "平台端-获取当前节点的父级节点")
    @GetMapping("getParentClassify")
    public JsonResult<WelfareGoodsClassify> getParentClassify(Integer id) {
        WelfareGoodsClassify welfareGoodsClassify = service.selectByPrimaryKey(id);
        if(EmptyUtils.isEmpty(welfareGoodsClassify)) {
            return JsonResult.paramErrWithMsg("当前节点不存在");
        }
        Integer parentId = welfareGoodsClassify.getParentId();
        if(parentId == 0) {
            return JsonResult.paramErrWithMsg("当前节点为第一级分类，没有父级分类！");
        }
        return JsonResult.success(service.selectByPrimaryKey(parentId));
    }

    /**
     * 获取当前节点
     * @date 2021/6/2 14:25
     * @author caobucao
     * @param id 当前节点id
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.goods.pojo.WelfareGoodsClassify>>
     */
    @ApiOperation(value = "平台端-获取当前节点", notes = "平台端-获取当前节点")
    @GetMapping("getClassify")
    public JsonResult<WelfareGoodsClassify> getClassify(Integer id) {
        WelfareGoodsClassify welfareGoodsClassify = service.selectByPrimaryKey(id);
        return JsonResult.success(welfareGoodsClassify);
    }

    /**
     * 保存商品分类（添加/编辑）
     * @date 2021/6/1 15:13
     * @author caobucao
     * @param welfareGoodsClassify 保存的分类对象
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "平台端-保存商品分类（添加/编辑）", notes = "平台端-保存商品分类（添加/编辑）")
    @PostMapping("saveGoodsClassify")
    public JsonResult<Integer> saveGoodsClassify(@RequestBody WelfareGoodsClassify welfareGoodsClassify) {
        if(EmptyUtils.isEmpty(welfareGoodsClassify.getName())) {
            return JsonResult.paramErrWithMsg("分类名称必填！");
        }
        if(EmptyUtils.isEmpty(welfareGoodsClassify.getSort())) {
            return JsonResult.paramErrWithMsg("排序号必填且不能为0！");
        }
        if(EmptyUtils.isEmpty(welfareGoodsClassify.getLevel())) {
            return JsonResult.paramErrWithMsg("分类等级必传！");
        }
        //如果是2级分类或者3级分类，则必传父级分类id
        if((welfareGoodsClassify.getLevel() == 2 || welfareGoodsClassify.getLevel() == 3)
                && EmptyUtils.isEmpty(welfareGoodsClassify.getParentId())) {
            return JsonResult.paramErrWithMsg("父级分类id必传且不能为0！");
        }
        //如果是1级分类，则默认父级分类id为0
        if(welfareGoodsClassify.getLevel() == 1) {
            welfareGoodsClassify.setParentId(0);
        }
        String userName = getAdminInfo().getUserName();
        if(EmptyUtils.isEmpty(welfareGoodsClassify.getId())) {
            welfareGoodsClassify.setCreaterName(userName);
            welfareGoodsClassify.setCreateTime(new Date());
            welfareGoodsClassify.setUpdaterName(userName);
            welfareGoodsClassify.setUpdateTime(new Date());
            welfareGoodsClassify.setIsShow(CommonConstants.YES);
            welfareGoodsClassify.setEnterpriseId(CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID);
            service.insertSelective(welfareGoodsClassify);
            syncInitDataServiceImpl.syncAddGoodsClassifyToCompany(welfareGoodsClassify.getId());
        } else {
            welfareGoodsClassify.setUpdaterName(userName);
            welfareGoodsClassify.setUpdateTime(new Date());
            service.updateByPrimaryKeySelective(welfareGoodsClassify);
        }
        return JsonResult.success();
    }

    /**
     * 更改分类是否显示
     * @date 2021/7/9 16:19
     * @author caobucao
     * @param jsonObject 传入参数 包含id和是否显示
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "平台端-更改分类是否显示", notes = "平台端-更改分类是否显示")
    @PostMapping("updateIsShowById")
    public JsonResult<Integer> updateIsShowById(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        Integer isShow = jsonObject.getInteger("isShow");
        WelfareGoodsClassify welfareGoodsClassify = new WelfareGoodsClassify();
        welfareGoodsClassify.setId(id);
        welfareGoodsClassify.setIsShow(isShow);
        welfareGoodsClassify.setUpdaterName(getAdminInfo().getUserName());
        welfareGoodsClassify.setUpdateTime(new Date());
        return JsonResult.success(service.updateByPrimaryKeySelective(welfareGoodsClassify));
    }

    /**
     * 逻辑删除分类
     * @date 2021/6/1 16:46
     * @author caobucao
     * @param id 主键id
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "平台端-逻辑删除分类", notes = "平台端-逻辑删除分类")
    @DeleteMapping("deleteGoodsClassify")
    public JsonResult<Integer> deleteGoodsClassify(Integer id) {
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("请传入id！");
        }
        return JsonResult.success(service.deleteGoodsClassify(id,getAdminInfo().getId()));
    }

}
